package com.eaoa.uc.pubmessage.action;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.base.EABaseAction;
import com.eaoa.constants.AttachmentConstants;
import com.eaoa.constants.PubAccountConstants;
import com.eaoa.query.pub.PubMessageQuery;
import com.eaoa.uc.pubmessage.form.MessageForm;
import com.eaoa.utils.AttachmentUtils;
import com.eaoa.vo.pub.PubMessage;
import com.stony.core.util.CoreUtils;

public class MessageModifyPreAction extends EABaseAction {

	protected ActionForward doWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		
		MessageForm myform = (MessageForm) form;
		Long messageno = CoreUtils.parseLong(myform.getMessageno());
		Long sessionid = CoreUtils.parseLong(myform.getSessionid());

		if (!isReEntry(request) ) {
			if(messageno!=null){
				PubMessage vo = PubMessageQuery.loadByNo(messageno);
				sessionid = vo.getSessionid();
				myform.setVO(vo);
			}
			
			if(getEmployee(request).getAccounttype().equals(PubAccountConstants.ACCOUNTTYPE_OUTER)){
				StringBuffer receivers = new StringBuffer();
				Iterator masterAccountIds = getEmployee(request).getMasterAccountids().iterator();
				while(masterAccountIds.hasNext()){
					receivers.append((Long)masterAccountIds.next()).append(",");
				}
				myform.setReceivers(receivers.toString());
				myform.setReceivername(PubAccountConstants.SERVICE_NAME);
			}
		}
		
		if(sessionid!=null){
			Map attachments = AttachmentUtils.getAttachmentMap(AttachmentConstants.ATTACHMENTTYPE_MESSAGE,sessionid);
			request.setAttribute("attachments", attachments);
		}
		


		return mapping.findForward("success");

	}
}
